﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using JohnnySins.DAL;
using JohnnySins.DAL.Repositories;
using JohnnySins.Entities;
using JohnnySins.Code;

namespace JohnnySins
{
    public partial class JoinNow : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ConfigurationManager.AppSettings["IsDevelopmentEnvironment"] == null)
            {
                string urlCurrent = Request.Url.ToString();
                if (urlCurrent.StartsWith("http:") == true)
                    Response.Redirect("https" + urlCurrent.Remove(0, 4), false);
            }
            
            //if (!IsPostBack)
            //    LoadCombo();

        }

        protected void LoadCombo()
        {
            //using (var repo = RepositoryFactory.ResolveRepository<UserRepository>())
            //{
            //    ddlCountry.DataSource = repo.CountryCombo(true, false);
            //    ddlCountry.DataBind();
            //}
        }

        protected void btnJoinNow_Click(object sender, EventArgs e)
        {
            if (!chkReed.Checked)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", string.Format("alert('{0}');", "You must agree with terms!"), true);
                return;
            }

            //string userName = Membership.GetUserNameByEmail(txtEmail.Text); //repo.Find(usr.UserName);

            //if (!String.IsNullOrEmpty(userName))
            //{
            //    var cookie = new HttpCookie("email", String.Empty);
            //    cookie.Expires = DateTime.Now.AddYears(1);
            //    Response.Cookies.Add(cookie);

            //    FormsAuthentication.SetAuthCookie(userName, false);


            //    Response.Redirect("~/member/Home.aspx", endResponse: true);

            //}

            //To Epoch

                string pi_code = string.Empty; 
                if (rbPaymentType2.Checked)
                {
                    pi_code = "ciirjj1p467145"; // 1 months
                }
                else if (rbPaymentType3.Checked)
                {
                    pi_code = "ciirjj2p467146"; // 3 months
                }
                else if (rbPaymentType4.Checked)
                {
                    pi_code = "ciirjj3p531975"; // 6 months
                }
            
                string api = "join";   //For new signups.  ‘memberplus’ value For MemberPlus. 
                string reseller = "a";   //lowercase letter ‘a’ from EPOCH documentation

                var data = new NameValueCollection();
                data.Add("pi_code", pi_code);
                data.Add("api", api);
                data.Add("reseller", reseller);
                //  data.Add("country", ddlCountry.SelectedItem.Text);
                data.Add("zip", txtZip.Text);
                data.Add("email", txtEmail.Text);
                data.Add("name", txtFirstName.Text);
                data.Add("password", txtPassword.Text);
                data.Add("pi_returnurl", EpochResponseUrl); //approval / denial response from FlexPost

                string postForm = CheckEpochMember.PreparePOSTForm(EpochRequestUrl, data);  //EpochRequestUrl

                Response.Clear();
                Response.Write(postForm);
                Response.End();
           
        }
        
        //private static String PreparePOSTForm(string url, NameValueCollection data)
        //{
        //    var sb = new StringBuilder();
        //    sb.Append("<html>");
        //    sb.AppendFormat("<body onload='document.forms[0].submit()'>");
        //    sb.AppendFormat("<form action='{0}' method='post'>", url);
        //    foreach (string key in data)
        //    {
        //        var str = data[key];
        //        sb.AppendFormat("<input type='hidden' name=" + key + " value=" + "'" + data[key] + "'" + ">");
        //    }
        //    sb.Append("</form>");
        //    sb.Append("</body>");
        //    sb.Append("</html>");

        //    return sb.ToString();
        //}

        //protected T GetFromWebConfig<T>(NameValueCollection section, string key, T defaultvalue)
        //{
        //    T ret = defaultvalue;

        //    if (section != null && !String.IsNullOrEmpty(section[key]))
        //    {
        //        ret = (T)Convert.ChangeType(section[key], typeof(T));
        //    }

        //    return ret;
        //}

        protected string EpochRequestUrl
        {
            get
            {
                return CheckEpochMember.GetFromWebConfig(ConfigurationManager.AppSettings, "EpochRequestUrl", String.Empty);
            }
        }

        protected string EpochResponseUrl
        {
            get
            {
                return CheckEpochMember.GetFromWebConfig(ConfigurationManager.AppSettings, "EpochResponseUrl", String.Empty);
            }
        }  

    }
}   

